﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using PMIS.Shared.DTO;
using PMIS.Shared.Models;
using PMIS.WebApi.Model;
using PMIS.WebApi.Services;
using PMIS.WebApi.SnowflakeId;
using PMIS.WebApi.Common.Attributes;
using Newtonsoft.Json;

namespace PMIS.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleController : ControllerBase
    {
        private readonly IMapper _mapper;
        private readonly IRoleService _service;
        private readonly IMenuAuthorizeService _menuAuthorizeService;
        private readonly SnowflakeIdGenerator _idGenerator;

        public RoleController(IRoleService service, 
            IMenuAuthorizeService menuAuthorizeService, 
            IMapper mapper, 
            SnowflakeIdGenerator idGenerator)
        {
            _service = service;
            _menuAuthorizeService = menuAuthorizeService;
            _mapper = mapper;
            _idGenerator = idGenerator;
        }

        [HttpPost]
        public async Task<IActionResult> Add(RoleDto r)
        {
            var res = new ApiResult();

            var list = await _service.GetEntityListAsync(null, null, x => x.Name == r.Name);
            if (list.Items.Count > 0)
            {
                res.code = -1;
                res.msg = "角色已存在";
                return Ok(res);
            }

            var role = _mapper.Map<Role>(r);
            role.Id = _idGenerator.GetId();
            var data = await _service.AddEntityAsync(role);
            res.data = data;
            return Ok(res);
        }

        [HttpPost]
        public async Task<IActionResult> SetRoleAuthorize(MenuAuthorizeDto m)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var menuList = m.MenuList;
            var itemList = new List<MenuAuthorize>();
            foreach (var menuId in menuList)
            {
                var id = _idGenerator.GetId();
                var item = new MenuAuthorize() { Id = id, RoleId = m.RoleId, MenuId = menuId };
                itemList.Add(item);
            }
            await _menuAuthorizeService.DeleteMenuAuthorize(m.RoleId);
            var data = await _menuAuthorizeService.AddMenuAuthorize(itemList);
            var res = new ApiResult() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> GetRoleAuthorizeList(string roleId)
        {
            var data = await _menuAuthorizeService.GetMenuAuthorizeList(roleId);
            List<string> menuIdList = new List<string>();
            foreach (var item in data)
            {
                menuIdList.Add(item.MenuId);
            }
            var res = new ApiResult() { data = menuIdList };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> GetMenuIdList()
        {
            var res = new ApiResult();
            var user = (User)HttpContext.Items["User"];
            if (user != null && user.RoleId != null)
            {
                List<string> roleIdList = JsonConvert.DeserializeObject<List<string>>(user.RoleId);
                var data = await _menuAuthorizeService.GetMenuIdList(roleIdList);
                res.data = data;
            }
            else
            {
                res.data = new List<string>();
            }
            return Ok(res);
        }

        [HttpPost]
        public async Task<IActionResult> Delete(List<string> ids)
        {
            var data = await _service.DeleteEntityAsync<Role>(x => ids.Contains(x.Id));
            var res = new ApiResult() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> Get(string id)
        {
            var data = await _service.GetEntityAsync(id);
            var res = new ApiResult() { data = data };
            return Ok(res);
        }

        [HttpPut]
        public async Task<IActionResult> Update(RoleDto r)
        {
            var res = new ApiResult();
            
            var list = await _service.GetEntityListAsync(null, null, x => x.Name == r.Name);
            if (list.Items.Count > 0)
            {
                res.code = -1;
                res.msg = "角色已存在";
                return Ok(res);
            }

            var role = _mapper.Map<Role>(r);
            var data = await _service.UpdateEntityAsync(role);
            res.data = data;
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> GetList(int? pageNum, int? pageSize)
        {
            var data = await _service.GetEntityListAsync(pageNum, pageSize);
            var res = new ApiResult() { data = data };
            return Ok(res);
        }
    }
}
